package com.study.provider.service.impl;

import com.study.provider.service.MetaSpaceOomMock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.stereotype.Service;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;

/**
 * @author anner
 * @description: TODO
 * @date 2021/4/18 22:57
 */
@Service
@Slf4j
public class MetaSpaceOomMockImpl implements MetaSpaceOomMock {

    /**
     * jvm内存模型问题
     * @param args
     */
    public static void main(String[] args) {
        ClassLoadingMXBean loadingMXBean = ManagementFactory.getClassLoadingMXBean();
        for (int i = 0; i < 2; i++){
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(MetaSpaceOomMock.class);
            enhancer.setCallbackTypes(new Class[]{Dispatcher.class, MethodInterceptor.class});
            enhancer.setCallbackFilter(new CallbackFilter() {
                public int accept(Method method) {
                    return 1;
                }

                public boolean equals(Object obj) {
                    return super.equals(obj);
                }
            });

            Class clazz = enhancer.createClass();
            log.info(clazz.getName() + "\n"
                + "total:" + loadingMXBean.getTotalLoadedClassCount()
                + "\nactive:" + loadingMXBean.getLoadedClassCount()
                + "\nunloaded:" + loadingMXBean.getUnloadedClassCount());
        }
    }

}
